﻿using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;

namespace OpenSoftStore.Client.BLL
{
    /// <summary>
    /// Cdatabase 的摘要说明。
    /// </summary>
    public class Cdatabase
    {
        private readonly OleDbConnection _accessConn = new OleDbConnection();
        public Cdatabase()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        #region"用于关闭连接"
        private void ConnClose()
        {
            if (ConnState())
            {
                _accessConn.Close();
            }
        }
        #endregion

        #region"ConnState 用于返回当前连接的状态"
        private bool ConnState()
        {
            if (_accessConn.State.ToString() == "Open")
            {
                return true;
            }
            return false;
        }

        #endregion

        #region"Open 用于打开数据库的方法"
        /// <summary>
        /// 用于打开Access数据库
        /// </summary>
        /// <returns>返回类型为 Bool 类型</returns>
        private bool Open()
        {
            try
            {
                string dbm =ConfigurationSettings.AppSettings["DataBaseName"];
                _accessConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbm; //+ ";User ID=Admin;Database Password=";
                _accessConn.Open();
                return true;
            }
            catch
            {
                return false;
            }
            //Msgbox._Show("数据库已经打开！",FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,this.Msgbox_Title );
            //System.Windows.Forms.MessageBox.Show("数据库已经打开！");
        }
        #endregion

        #region"RunSQLcmd 用于运行SQL语句的方法"
        /// <summary>
        /// 用于运行SQL语句的方法
        /// 返回值为一个bool
        /// </summary>
        /// <param name="cmdStr">要运行的 SQL 语句</param>
        /// <returns>返回值为 bool 类型 </returns>
        public bool RunSqLcmd(string cmdStr)
        {
            try
            {
                if (Open())
                {
                    var cmd1 = new OleDbCommand("", _accessConn) {CommandText = cmdStr};
                    cmd1.ExecuteNonQuery();//运行SQL语句
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "RunSQLcmdDataSet 用于运行SQL语句的方法"
        /// <summary>
        /// 用于运行SQL语句的方法
        /// 返回值为一个DataSet
        /// 注意此方法与 RunSQLcmd 的不同
        /// </summary>
        /// <param name="cmdStr">要运行的 SQL 语句</param>
        /// <returns>返回值为 DataSet 类型 </returns>
        public DataSet RunSqLcmdDataSet(string cmdStr)
        {
            try
            {
                if (Open())
                {
                    var myDataSet = new DataSet();
                    var myaccessdp = new OleDbDataAdapter
                    {
                        SelectCommand = new OleDbCommand(cmdStr, _accessConn)
                    };
                    myaccessdp.Fill(myDataSet);
                    return myDataSet;
                }
                return null;
            }
            catch (Exception ex)
            {
                // Msgbox._Show(Ex.Message,FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,"在runsqlcmd的方法中出错");
                //System.Windows.Forms.MessageBox.Show(Ex.Message,"在runsqlcmd的方法中出错");
                return null;
            }
            finally
            {
                ConnClose();
            }
        }
        #endregion

        #region "用于把一个表的数据绑定到一个下拉列表框中去"
        /// <summary>
        /// 用于把一个表的数据绑定到一个下拉列表框中去
        /// </summary>
        /// <param name="youDropDownList">要绑定的下拉列表框</param>
        /// <param name="sqlcmd">SQL命令</param>
        /// <param name="showText">下列要显示的值</param>
        /// <param name="showValue">下列实际的值</param>
        //public void BingDataToDropDownList(System.Web.UI.WebControls.DropDownList youDropDownList, string sqlcmd, string showText, string showValue)
        //{
        //    DataTable temptable = RunSqLcmdDataSet(sqlcmd).Tables[0];
        //    youDropDownList.DataSource = temptable;
        //    youDropDownList.DataTextField = showText;
        //    youDropDownList.DataValueField = showValue;
        //    youDropDownList.DataBind();
        //}
        #endregion

    }
}
